package com.gxuwz.web.struts2;

import java.util.List;

import javax.annotation.Resource;

import com.gxuwz.annotation.LogMsg;
import com.gxuwz.entity.SysDepartment;
import com.gxuwz.entity.SysLaboratory;
import com.gxuwz.entity.SysUser;
import com.gxuwz.service.IDepartmentService;
import com.gxuwz.service.ILaboratoryService;
import com.gxuwz.util.DateUtils;
import com.gxuwz.util.Page;
import com.gxuwz.web.AbstractBaseController;
import com.gxuwz.web.IBaseController;

/**
 * 实验室控制器
 * @author 小胡  
 * @date 2017年5月28日
 */
public class LaboratoryController extends AbstractBaseController implements IBaseController{

	private static final long serialVersionUID = -4651994770715688875L;
	private List<Integer> ids;	//	获取复选框的值
	private SysLaboratory laboratory;
	
	@Resource(name = "laboratoryService")
	private ILaboratoryService laboratoryService;
	@Resource(name = "departmentService")
	private IDepartmentService departmentService;
	
	
	/**
	 * 获取部门列表
	 * @date 2017年5月30日
	 */
	public List<SysDepartment> getDepartments(){
			return departmentService.getAllFromTable();
	}
	
	/**
	 * 实验室列表
	 * @return
	 */
	@Override
	public String listPrompt(){
		//创建分页对象指定泛型
		Page<SysLaboratory> page = new Page<SysLaboratory>();
		//设置单页记录数
		page.setOnePageNum(10);
		//设置当前页数 默认1
		page.setPageNo(pageNo==null?1:pageNo);
		page.setClazz("Laboratory");
		laboratoryService.findForPage(page);		
		request.put("page", page);		
		setPrompt("/WEB-INF/pages/laboratory/laboratory_list.jsp");
		return SUCCESS;
	}
	
	@Override
	public String addPrompt(){
		session.put("departments", getDepartments());	// 将部门数据传到前台
		setPrompt("/WEB-INF/pages/laboratory/laboratory_save.jsp");
		return SUCCESS;
	}
	@Override
	@LogMsg(msg = "新增实验室")
	public String doSave(){
		SysUser user = (SysUser) session.get("user");
		laboratory.setCreateUserName(user.getUserName());	
		laboratory.setCreateDate(DateUtils.getCurrentDate());
		laboratoryService.save(laboratory);
		setPrompt("Laboratory_listPrompt.action");
		return "reposition";
	}
	
	@Override
	@LogMsg(msg = "删除实验室")
	public String doBatchDelete(){
		laboratoryService.batchDelete(ids);
		setPrompt("Laboratory_listPrompt.action");
		return "reposition";
	}
	
	@Override
	public String updatePrompt(){	
		Integer id = laboratory.getId();
		SysLaboratory lab = laboratoryService.load(id);
		session.put("updateLaboratory", lab);
		session.put("departments", getDepartments());
		setPrompt("/WEB-INF/pages/laboratory/laboratory_update.jsp");
		return SUCCESS;
	}
	@Override
	@LogMsg(msg = "更新实验室")
	public String doUpdate(){
		SysUser user = (SysUser) session.get("user");
		laboratory.setCreateUserName(user.getUserName());
		laboratory.setCreateDate(DateUtils.getCurrentDate());
		laboratory.setDepartment(departmentService.load(laboratory.getDepartment().getId()));
		laboratoryService.update(laboratory);
		setPrompt("Laboratory_listPrompt.action");
		return "reposition";
	}
	

	
	
	public List<Integer> getIds() {
		return ids;
	}
	
	public void setIds(List<Integer> ids) {
		this.ids = ids;
	}

	public SysLaboratory getLaboratory() {
		return laboratory;
	}

	public void setLaboratory(SysLaboratory laboratory) {
		this.laboratory = laboratory;
	}

}
